﻿using iNKORE.UI.WPF.Modern.Controls;
using System.Windows;

namespace iNKORE.UI.WPF.Modern.Gallery.Pages.Controls.Windows
{
    public partial class CheckBoxPage : Page
    {
        public CheckBoxPage()
        {
            InitializeComponent();
            Loaded += CheckBoxPage_Loaded;
        }

        void CheckBoxPage_Loaded(object sender, RoutedEventArgs e)
        {
            SetCheckedState();
            UpdateExampleCode();
        }

        private void Control1_Checked(object sender, RoutedEventArgs e)
        {
            Control1Output.Text = "You checked the box.";
        }

        private void Control1_Unchecked(object sender, RoutedEventArgs e)
        {
            Control1Output.Text = "You unchecked the box.";
        }

        private void Control2_Checked(object sender, RoutedEventArgs e)
        {
            Control2Output.Text = "CheckBox is checked.";
        }

        private void Control2_Unchecked(object sender, RoutedEventArgs e)
        {
            Control2Output.Text = "CheckBox is unchecked.";
        }

        private void Control2_Indeterminate(object sender, RoutedEventArgs e)
        {
            Control2Output.Text = "CheckBox state is indeterminate.";
        }

        #region SelectAllMethods
        private void SelectAll_Checked(object sender, RoutedEventArgs e)
        {
            Option1CheckBox.IsChecked = Option2CheckBox.IsChecked = Option3CheckBox.IsChecked = true;
        }

        private void SelectAll_Unchecked(object sender, RoutedEventArgs e)
        {
            Option1CheckBox.IsChecked = Option2CheckBox.IsChecked = Option3CheckBox.IsChecked = false;
        }

        private void SelectAll_Indeterminate(object sender, RoutedEventArgs e)
        {
            // If the SelectAll box is checked (all options are selected),
            // clicking the box will change it to its indeterminate state.
            // Instead, we want to uncheck all the boxes,
            // so we do this programatically. The indeterminate state should
            // only be set programatically, not by the user.

            if (Option1CheckBox.IsChecked == true &&
                Option2CheckBox.IsChecked == true &&
                Option3CheckBox.IsChecked == true)
            {
                // This will cause SelectAll_Unchecked to be executed, so
                // we don't need to uncheck the other boxes here.
                OptionsAllCheckBox.IsChecked = false;
            }
        }

        private void SetCheckedState()
        {
            // Controls are null the first time this is called, so we just
            // need to perform a null check on any one of the controls.
            if (Option1CheckBox != null)
            {
                if (Option1CheckBox.IsChecked == true &&
                    Option2CheckBox.IsChecked == true &&
                    Option3CheckBox.IsChecked == true)
                {
                    OptionsAllCheckBox.IsChecked = true;
                }
                else if (Option1CheckBox.IsChecked == false &&
                    Option2CheckBox.IsChecked == false &&
                    Option3CheckBox.IsChecked == false)
                {
                    OptionsAllCheckBox.IsChecked = false;
                }
                else
                {
                    // Set third state (indeterminate) by setting IsChecked to null.
                    OptionsAllCheckBox.IsChecked = null;
                }
            }
        }

        private void Option_Checked(object sender, RoutedEventArgs e)
        {
            SetCheckedState();
        }

        private void Option_Unchecked(object sender, RoutedEventArgs e)
        {
            SetCheckedState();
        }
        #endregion

        #region Example Code

        public void UpdateExampleCode()
        {
            Example1.Xaml = Example1Xaml;
            Example2.Xaml = Example2Xaml;
            Example3.Xaml = Example3Xaml;
            Example3.CSharp = Example3CS;
        }

        public string Example1Xaml => $@"
<CheckBox Content=""Two-state CheckBox""
    Checked=""Control1_Checked""
    Unchecked=""Control1_Unchecked"" />
";

        public string Example2Xaml => $@"
<CheckBox Content=""Three-state CheckBox""
    Checked=""Control2_Checked""
    Indeterminate=""Control2_Indeterminate""
    IsThreeState=""True""
    Unchecked=""Control2_Unchecked"" />
";

        public string Example3Xaml => $@"
<StackPanel>
    <CheckBox
        x:Name=""OptionsAllCheckBox""
        Checked=""SelectAll_Checked""
        Content=""Select all""
        Indeterminate=""SelectAll_Indeterminate""
        IsThreeState=""True""
        Unchecked=""SelectAll_Unchecked"" />
    <CheckBox
        x:Name=""Option1CheckBox""
        Margin=""24,0,0,0""
        Checked=""Option_Checked""
        Content=""Option 1""
        Unchecked=""Option_Unchecked"" />
    <CheckBox
        x:Name=""Option2CheckBox""
        Margin=""24,0,0,0""
        Checked=""Option_Checked""
        Content=""Option 2""
        IsChecked=""True""
        Unchecked=""Option_Unchecked"" />
    <CheckBox
        x:Name=""Option3CheckBox""
        Margin=""24,0,0,0""
        Checked=""Option_Checked""
        Content=""Option 3""
        Unchecked=""Option_Unchecked"" />
</StackPanel>
";

        public string Example3CS => $@"
private void SelectAll_Checked(object sender, RoutedEventArgs e)
{{
    Option1CheckBox.IsChecked = Option2CheckBox.IsChecked = Option3CheckBox.IsChecked = true;
}}

private void SelectAll_Unchecked(object sender, RoutedEventArgs e)
{{
    Option1CheckBox.IsChecked = Option2CheckBox.IsChecked = Option3CheckBox.IsChecked = false;
}}

private void SelectAll_Indeterminate(object sender, RoutedEventArgs e)
{{
    // If the SelectAll box is checked (all options are selected),
    // clicking the box will change it to its indeterminate state.
    // Instead, we want to uncheck all the boxes,
    // so we do this programatically. The indeterminate state should
    // only be set programatically, not by the user.

    if (Option1CheckBox.IsChecked == true &&
        Option2CheckBox.IsChecked == true &&
        Option3CheckBox.IsChecked == true)
    {{
        // This will cause SelectAll_Unchecked to be executed, so
        // we don't need to uncheck the other boxes here.
        OptionsAllCheckBox.IsChecked = false;
    }}
}}

private void SetCheckedState()
{{
    // Controls are null the first time this is called, so we just
    // need to perform a null check on any one of the controls.
    if (Option1CheckBox != null)
    {{
        if (Option1CheckBox.IsChecked == true &&
            Option2CheckBox.IsChecked == true &&
            Option3CheckBox.IsChecked == true)
        {{
            OptionsAllCheckBox.IsChecked = true;
        }}
        else if (Option1CheckBox.IsChecked == false &&
            Option2CheckBox.IsChecked == false &&
            Option3CheckBox.IsChecked == false)
        {{
            OptionsAllCheckBox.IsChecked = false;
        }}
        else
        {{
            // Set third state (indeterminate) by setting IsChecked to null.
            OptionsAllCheckBox.IsChecked = null;
        }}
    }}
}}

private void Option_Checked(object sender, RoutedEventArgs e)
{{
    SetCheckedState();
}}

private void Option_Unchecked(object sender, RoutedEventArgs e)
{{
    SetCheckedState();
}}
";

        #endregion

    }
}
